package com.elite.controller;

import com.alibaba.fastjson.JSON;
import com.elite.entity.File;
import com.elite.entity.FileGoods;
import com.elite.entity.User;
import com.elite.service.FileService;
import com.elite.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by hbl on 2017/1/13.
 */
@RestController
@RequestMapping("/user")

public class UserController {
    //引入log4j进行打印
    private static final Logger LOGGER = Logger.getLogger(UserController.class);
    @Autowired
    UserService userService;
    String result;

    @Autowired
    private HttpSession session;
    @Autowired
    private FileService fileService;



    @RequestMapping(value = "/test",method = RequestMethod.GET)
    public String test(){
        return "/login.html";
    }
    /**
     * 用户注册
     * @param userName
     * @param userNikename
     * @param userPhone
     * @param userSchool
     * @param userPassword
     */

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public String login(String userName,String userNikename,String userPhone,String userSchool,String userPassword){

        User user = new User();
        String number = userPhone;
        List<User> Phone=userService.selectByPhone(number);
        int count = Phone.size();
        if (count>0){
            result = "false";
        }else {
            String userAccount = String.valueOf(UUID.randomUUID());
            user.setUserName(userName);
            user.setUserNikename(userNikename);
            user.setUserPhone(userPhone);
            user.setUserSchool(userSchool);
            user.setUserPassword(userPassword);
            user.setUserAccount(userAccount);
            userService.addUser(user);
            result = "true";
        }
        return JSON.toJSON(result).toString()+JSON.toJSON(result).toString();
    }

    /**
     *用户 登录
     * @param userPhone
     * @param password
     */
    @RequestMapping(value = "/loginUser")
    public String loginUser(String userPhone,String password){

        String thePhone = userPhone;
        String theWord = password;
        List<User> phone = userService.selectByPhone(userPhone);

        if(phone.size()==0) {

            result = "none";
            return JSON.toJSON(result).toString();
        }else{

            User user = phone.get(0);
            if(user.getUserPhone().equals(thePhone) && user.getUserPassword().equals(theWord)){

                result = "true";
                session.setAttribute("loginUser",user);
                return JSON.toJSON(user).toString();

            }else{

                result = "false";
                return JSON.toJSON(result).toString();
            }
        }
    }
    /**
     * 通过id修改资料
     * @param userId
     * @param userPassword
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(Integer userId,String userName,String userNikename,String userPhone,String userSchool,String userPassword){
        User userRecord = new User();
        userRecord.setUserId(userId);
        userRecord.setUserName(userName);
        userRecord.setUserNikename(userNikename);
        userRecord.setUserPhone(userPhone);
        userRecord.setUserSchool(userSchool);
        userRecord.setUserPassword(userPassword);
        userService.update(userRecord);
        result = "ok";
        return result;
    }

    /**
     * 通过id查找昵称
     * @param userId
     * @return
     */
    @RequestMapping(value = "/SetNickname",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String setNickname(Integer userId){
        User user = userService.selectById(userId);
        String nickName = user.getUserNikename();
        return JSON.toJSON(nickName).toString();
    }

    /**
     * 个人资料里面返回这个id的所有信息
     * @param userId
     * @return
     */
    @RequestMapping(value = "/setNameAndPhone",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String setNameAndPhone(Integer userId){
        User user = userService.selectById(userId);
        return JSON.toJSON(user).toString();
    }

    @RequestMapping(value = "/getImg",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String getImg(){

        User user = (User)session.getAttribute("loginUser");
        Integer userId = user.getUserId();
        FileGoods fileGoods = null;
        try {
            fileGoods = fileService.selectByUserId(userId);
        }catch (NullPointerException e){
            e.printStackTrace();
        }

        File file = null;
        if (fileGoods != null){
            file = fileService.selectByPrimaryKey(fileGoods.getFileId());
        }
        Map<String,Object> map = new HashMap<String, Object>();
        if (file != null){
            map.put("fileUrl",file.getFileUrl());
        }
        return JSON.toJSON(map).toString();
    }





}
